#include "Config.h"

#include "sdk.h"
#ifndef CB_PRECOMP
#include <wx/filename.h>

#include <configmanager.h>
#include <manager.h>
#include <personalitymanager.h>
#endif
#include <wx/fileconf.h>


DoxyBlocksConfig::DoxyBlocksConfig() {
  m_iBlockComment = GetDefaultBlockComment();
  m_iLineComment = GetDefaultLineComment();
  m_sProjectNumber = GetDefaultProjectNumber();
  m_bUseAutoVersion = GetDefaultUseAutoVersion();
  m_sOutputDirectory = GetDefaultOutputDirectory();
  m_sOutputLanguage = GetDefaultOutputLanguage();
  m_bExtractAll = GetDefaultExtractAll();
  m_bExtractPrivate = GetDefaultExtractPrivate();
  m_bExtractStatic = GetDefaultExtractStatic();
  m_bWarnings = GetDefaultWarnings();
  m_bWarnIfDocError = GetDefaultWarnIfDocError();
  m_bWarnIfUndocumented = GetDefaultWarnIfUndocumented();
  m_bWarnNoParamdoc = GetDefaultWarnNoParamdoc();
  m_bAlphabeticalIndex = GetDefaultAlphabeticalIndex();
  m_bGenerateHTML = GetDefaultGenerateHTML();
  m_bGenerateHTMLHelp = GetDefaultGenerateHTMLHelp();
  m_bGenerateCHI = GetDefaultGenerateCHI();
  m_bBinaryTOC = GetDefaultBinaryTOC();
  m_bGenerateLatex = GetDefaultGenerateLatex();
  m_bGenerateRTF = GetDefaultGenerateRTF();
  m_bGenerateMan = GetDefaultGenerateMan();
  m_bGenerateXML = GetDefaultGenerateXML();
  m_bGenerateAutogenDef = GetDefaultGenerateAutogenDef();
  m_bGeneratePerlMod = GetDefaultGeneratePerlMod();
  m_bEnablePreprocessing = GetDefaultEnablePreprocessing();
  m_bClassDiagrams = GetDefaultClassDiagrams();
  m_bHaveDot = GetDefaultHaveDot();
  m_sPathDoxygen = GetDefaultPathDoxygen();
  m_sPathDoxywizard = GetDefaultPathDoxywizard();
  m_sPathHHC = GetDefaultPathHHC();
  m_sPathDot = GetDefaultPathDot();
  m_sPathCHMViewer = GetDefaultPathCHMViewer();
  m_bOverwriteDoxyfile = GetDefaultOverwriteDoxyfile();
  m_bPromptBeforeOverwriting = GetDefaultPromptBeforeOverwriting();
  m_bUseAtInTags = GetDefaultUseAtInTags();
  m_bLoadTemplate = GetDefaultLoadTemplate();
  m_bUseInternalViewer = GetDefaultUseInternalViewer();
  m_bRunHTML = GetDefaultRunHTML();
  m_bRunCHM = GetDefaultRunCHM();
}


bool DoxyBlocksConfig::WritePrefsTemplate() {
  wxString sSectionCommentStyle( wxT( "/CommentStyle/" ) );
  wxString sSectionProject( wxT( "/Project/" ) );
  wxString sSectionBuild( wxT( "/Build/" ) );
  wxString sSectionWarnings( wxT( "/Warnings/" ) );
  wxString sSectionAlphabeticalClassIndex( wxT( "/AlphabeticalClassIndex/" ) );
  wxString sSectionOutput( wxT( "/Output/" ) );
  wxString sSectionPreProcessor( wxT( "/PreProcessor/" ) );
  wxString sSectionDot( wxT( "/Dot/" ) );
  wxString sSectionGeneral( wxT( "/General/" ) );
  wxString sCommentBlock( wxT( "Block" ) );
  wxString sCommentLine( wxT( "Line" ) );
  wxString sUseAutoVersion( wxT( "UseAutoVersion" ) );
  wxString sOutputLanguage( wxT( "OutputLanguage" ) );
  wxString sExtractAll( wxT( "ExtractAll" ) );
  wxString sExtractPrivate( wxT( "ExtractPrivate" ) );
  wxString sExtractStatic( wxT( "ExtractStatic" ) );
  wxString sWarnings( wxT( "Warnings" ) );
  wxString sWarnIfDocError( wxT( "WarnIfDocError" ) );
  wxString sWarnIfUndocumented( wxT( "WarnIfUndocumented" ) );
  wxString sWarnNoParamDoc( wxT( "WarnNoParamDoc" ) );
  wxString sAlphabeticalIndex( wxT( "AlphabeticalIndex" ) );
  wxString sGenerateHTML( wxT( "GenerateHTML" ) );
  wxString sGenerateHTMLHelp( wxT( "GenerateHTMLHelp" ) );
  wxString sGenerateCHI( wxT( "GenerateCHI" ) );
  wxString sBinaryTOC( wxT( "BinaryTOC" ) );
  wxString sGenerateLatex( wxT( "GenerateLatex" ) );
  wxString sGenerateRTF( wxT( "GenerateRTF" ) );
  wxString sGenerateMan( wxT( "GenerateMan" ) );
  wxString sGenerateXML( wxT( "GenerateXML" ) );
  wxString sGenerateAutogenDef( wxT( "GenerateAutogenDef" ) );
  wxString sGeneratePerlMod( wxT( "GeneratePerlMod" ) );
  wxString sEnablePreProcessing( wxT( "EnablePreProcessing" ) );
  wxString sClassDiagrams( wxT( "ClassDiagrams" ) );
  wxString sHaveDot( wxT( "HaveDot" ) );
  wxString sUseAtInTags( wxT( "UseAtInTags" ) );
  wxString sCfgIni( wxT( "DoxyBlocks.ini" ) );
  wxString sCfgPath( Manager::Get()->GetConfigManager( _T( "app" ) )->GetConfigFolder() + wxFILE_SEP_PATH + sCfgIni );
  wxFileConfig *cfgFile = new wxFileConfig( wxEmptyString, wxEmptyString, sCfgPath, wxEmptyString, wxCONFIG_USE_GLOBAL_FILE );
  cfgFile->Write( sSectionCommentStyle + sCommentBlock, static_cast<int>( m_iBlockComment ) );
  cfgFile->Write( sSectionCommentStyle + sCommentLine, static_cast<int>( m_iLineComment ) );
  cfgFile->Write( sSectionProject + sUseAutoVersion, static_cast<int>( m_bUseAutoVersion ) );
  cfgFile->Write( sSectionProject + sOutputLanguage, m_sOutputLanguage );
  cfgFile->Write( sSectionBuild + sExtractAll, static_cast<int>( m_bExtractAll ) );
  cfgFile->Write( sSectionBuild + sExtractPrivate, static_cast<int>( m_bExtractPrivate ) );
  cfgFile->Write( sSectionBuild + sExtractStatic, static_cast<int>( m_bExtractStatic ) );
  cfgFile->Write( sSectionWarnings + sWarnings, static_cast<int>( m_bWarnings ) );
  cfgFile->Write( sSectionWarnings + sWarnIfDocError, static_cast<int>( m_bWarnIfDocError ) );
  cfgFile->Write( sSectionWarnings + sWarnIfUndocumented, static_cast<int>( m_bWarnIfUndocumented ) );
  cfgFile->Write( sSectionWarnings + sWarnNoParamDoc, static_cast<int>( m_bWarnNoParamdoc ) );
  cfgFile->Write( sSectionAlphabeticalClassIndex + sAlphabeticalIndex, static_cast<int>( m_bAlphabeticalIndex ) );
  cfgFile->Write( sSectionOutput + sGenerateHTML, static_cast<int>( m_bGenerateHTML ) );
  cfgFile->Write( sSectionOutput + sGenerateHTMLHelp, static_cast<int>( m_bGenerateHTMLHelp ) );
  cfgFile->Write( sSectionOutput + sGenerateCHI, static_cast<int>( m_bGenerateCHI ) );
  cfgFile->Write( sSectionOutput + sBinaryTOC, static_cast<int>( m_bBinaryTOC ) );
  cfgFile->Write( sSectionOutput + sGenerateLatex, static_cast<int>( m_bGenerateLatex ) );
  cfgFile->Write( sSectionOutput + sGenerateRTF, static_cast<int>( m_bGenerateRTF ) );
  cfgFile->Write( sSectionOutput + sGenerateMan, static_cast<int>( m_bGenerateMan ) );
  cfgFile->Write( sSectionOutput + sGenerateXML, static_cast<int>( m_bGenerateXML ) );
  cfgFile->Write( sSectionOutput + sGenerateAutogenDef, static_cast<int>( m_bGenerateAutogenDef ) );
  cfgFile->Write( sSectionOutput + sGeneratePerlMod, static_cast<int>( m_bGeneratePerlMod ) );
  cfgFile->Write( sSectionPreProcessor + sEnablePreProcessing, static_cast<int>( m_bEnablePreprocessing ) );
  cfgFile->Write( sSectionDot + sClassDiagrams, static_cast<int>( m_bClassDiagrams ) );
  cfgFile->Write( sSectionDot + sHaveDot, static_cast<int>( m_bHaveDot ) );
  cfgFile->Write( sSectionGeneral + sUseAtInTags, static_cast<int>( m_bUseAtInTags ) );
  bool bOK = cfgFile->Flush();
  wxDELETE( cfgFile );
  wxASSERT( !cfgFile );
  return bOK;
}


bool DoxyBlocksConfig::ReadPrefsTemplate() {
  wxString sSectionCommentStyle( wxT( "/CommentStyle/" ) );
  wxString sSectionProject( wxT( "/Project/" ) );
  wxString sSectionBuild( wxT( "/Build/" ) );
  wxString sSectionWarnings( wxT( "/Warnings/" ) );
  wxString sSectionAlphabeticalClassIndex( wxT( "/AlphabeticalClassIndex/" ) );
  wxString sSectionOutput( wxT( "/Output/" ) );
  wxString sSectionPreProcessor( wxT( "/PreProcessor/" ) );
  wxString sSectionDot( wxT( "/Dot/" ) );
  wxString sSectionGeneral( wxT( "/General/" ) );
  wxString sCommentBlock( wxT( "Block" ) );
  wxString sCommentLine( wxT( "Line" ) );
  wxString sUseAutoVersion( wxT( "UseAutoVersion" ) );
  wxString sOutputLanguage( wxT( "OutputLanguage" ) );
  wxString sExtractAll( wxT( "ExtractAll" ) );
  wxString sExtractPrivate( wxT( "ExtractPrivate" ) );
  wxString sExtractStatic( wxT( "ExtractStatic" ) );
  wxString sWarnings( wxT( "Warnings" ) );
  wxString sWarnIfDocError( wxT( "WarnIfDocError" ) );
  wxString sWarnIfUndocumented( wxT( "WarnIfUndocumented" ) );
  wxString sWarnNoParamDoc( wxT( "WarnNoParamDoc" ) );
  wxString sAlphabeticalIndex( wxT( "AlphabeticalIndex" ) );
  wxString sGenerateHTML( wxT( "GenerateHTML" ) );
  wxString sGenerateHTMLHelp( wxT( "GenerateHTMLHelp" ) );
  wxString sGenerateCHI( wxT( "GenerateCHI" ) );
  wxString sBinaryTOC( wxT( "BinaryTOC" ) );
  wxString sGenerateLatex( wxT( "GenerateLatex" ) );
  wxString sGenerateRTF( wxT( "GenerateRTF" ) );
  wxString sGenerateMan( wxT( "GenerateMan" ) );
  wxString sGenerateXML( wxT( "GenerateXML" ) );
  wxString sGenerateAutogenDef( wxT( "GenerateAutogenDef" ) );
  wxString sGeneratePerlMod( wxT( "GeneratePerlMod" ) );
  wxString sEnablePreProcessing( wxT( "EnablePreProcessing" ) );
  wxString sClassDiagrams( wxT( "ClassDiagrams" ) );
  wxString sHaveDot( wxT( "HaveDot" ) );
  wxString sUseAtInTags( wxT( "UseAtInTags" ) );
  wxString sCfgIni( wxT( "DoxyBlocks.ini" ) );
  wxString sCfgPath( Manager::Get()->GetConfigManager( _T( "app" ) )->GetConfigFolder() + wxFILE_SEP_PATH + sCfgIni );
  if( !wxFile::Exists( sCfgPath ) ) {
    return false;
  }
  wxFileConfig *cfgFile = new wxFileConfig( wxEmptyString, wxEmptyString, sCfgPath, wxEmptyString, wxCONFIG_USE_GLOBAL_FILE );
  m_iBlockComment = cfgFile->Read( sSectionCommentStyle + sCommentBlock, GetDefaultBlockComment() );
  m_iLineComment = cfgFile->Read( sSectionCommentStyle + sCommentLine, GetDefaultLineComment() );
  m_bUseAutoVersion = cfgFile->Read( sSectionProject + sUseAutoVersion, GetDefaultUseAutoVersion() );
  m_sOutputLanguage = cfgFile->Read( sSectionProject + sOutputLanguage, GetDefaultOutputLanguage() );
  m_bExtractAll = cfgFile->Read( sSectionBuild + sExtractAll, GetDefaultExtractAll() );
  m_bExtractPrivate = cfgFile->Read( sSectionBuild + sExtractPrivate, GetDefaultExtractPrivate() );
  m_bExtractStatic = cfgFile->Read( sSectionBuild + sExtractStatic, GetDefaultExtractStatic() );
  m_bWarnings = cfgFile->Read( sSectionWarnings + sWarnings, GetDefaultWarnings() );
  m_bWarnIfDocError = cfgFile->Read( sSectionWarnings + sWarnIfDocError, GetDefaultWarnIfDocError() );
  m_bWarnIfUndocumented = cfgFile->Read( sSectionWarnings + sWarnIfUndocumented, GetDefaultWarnIfUndocumented() );
  m_bWarnNoParamdoc = cfgFile->Read( sSectionWarnings + sWarnNoParamDoc, GetDefaultWarnNoParamdoc() );
  m_bAlphabeticalIndex = cfgFile->Read( sSectionAlphabeticalClassIndex + sAlphabeticalIndex, GetDefaultAlphabeticalIndex() );
  m_bGenerateHTML = cfgFile->Read( sSectionOutput + sGenerateHTML, GetDefaultGenerateHTML() );
  m_bGenerateHTMLHelp = cfgFile->Read( sSectionOutput + sGenerateHTMLHelp, GetDefaultGenerateHTMLHelp() );
  m_bGenerateCHI = cfgFile->Read( sSectionOutput + sGenerateCHI, GetDefaultGenerateCHI() );
  m_bBinaryTOC = cfgFile->Read( sSectionOutput + sBinaryTOC, GetDefaultBinaryTOC() );
  m_bGenerateLatex = cfgFile->Read( sSectionOutput + sGenerateLatex, GetDefaultGenerateLatex() );
  m_bGenerateRTF = cfgFile->Read( sSectionOutput + sGenerateRTF, GetDefaultGenerateRTF() );
  m_bGenerateMan = cfgFile->Read( sSectionOutput + sGenerateMan, GetDefaultGenerateMan() );
  m_bGenerateXML = cfgFile->Read( sSectionOutput + sGenerateXML, GetDefaultGenerateXML() );
  m_bGenerateAutogenDef = cfgFile->Read( sSectionOutput + sGenerateAutogenDef, GetDefaultGenerateAutogenDef() );
  m_bGeneratePerlMod = cfgFile->Read( sSectionOutput + sGeneratePerlMod, GetDefaultGeneratePerlMod() );
  m_bEnablePreprocessing = cfgFile->Read( sSectionPreProcessor + sEnablePreProcessing, GetDefaultEnablePreprocessing() );
  m_bClassDiagrams = cfgFile->Read( sSectionDot + sClassDiagrams, GetDefaultClassDiagrams() );
  m_bHaveDot = cfgFile->Read( sSectionDot + sHaveDot, GetDefaultHaveDot() );
  m_bUseAtInTags = cfgFile->Read( sSectionGeneral + sUseAtInTags, GetDefaultUseAtInTags() );
  wxDELETE( cfgFile );
  wxASSERT( !cfgFile );
  return true;
}
